package cn.tianyigps.tianyigpselectricfence.customer;

import cn.tianyigps.tianyigpselectricfence.handler.ElectricFenceHandler;
import cn.tianyigps.tianyigpskafka.task.customer.NewCustomer;
import cn.tianyigps.tianyigpspublic.param.bo.Task;
import cn.tianyigps.tianyigpspublic.param.constant.EquipmentReceiveConstant;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Map;


/**
 * @Author: WEI.YUAN
 * @Descirption:电子围栏消费者
 * @Date: 2019/4/11_10:47
 */
@Component
@Slf4j
public class ElectricFenceCustomer extends NewCustomer {
    @Autowired
    ElectricFenceHandler electricFenceHandler;

    @Override
    public boolean checkTask(Task task) {
        if (CollectionUtils.isEmpty(task.getParams())) {
            return false;
        }
        //task的Type不能为空
        if (StringUtils.isEmpty(task.getType())) {
            return false;
        }
        //new不能为空
        if (task.getNext() == null) {
            return false;
        }
        Task nextTask = task.getNext();
        //nextTask的Type不能为空
        if (StringUtils.isEmpty(nextTask.getType())) {
            return false;
        }
        //next的callback不能为空
        if (!nextTask.getParams().containsKey("url") || StringUtils.isEmpty(nextTask.getParams().get("url"))) {
            return false;
        }
        //设备号不能为空
        Map<String, String> equipmentIdsMap = task.getParams();
        if (!equipmentIdsMap.containsKey(EquipmentReceiveConstant.DeviceCode) || StringUtils.isEmpty(EquipmentReceiveConstant.DeviceCode)) {
            return false;
        }
        //经纬度不能为空
        if (!equipmentIdsMap.containsKey(EquipmentReceiveConstant.Latitude) || StringUtils.isEmpty(EquipmentReceiveConstant.Latitude)) {
            return false;
        }
        if (!equipmentIdsMap.containsKey(EquipmentReceiveConstant.Longitude) || StringUtils.isEmpty(EquipmentReceiveConstant.Longitude)) {
            return false;
        }
        return true;
    }

    @Override
    public void consume(Task task) {
        electricFenceHandler.checkElectricFence(task);

    }

    //    private static ObjectMapper OM = new ObjectMapper();
//
//    /**
//     * kafka监听反序列化数据
//     * @param json
//     */
//    @KafkaListener(topics = {"electricefencetask"}, containerFactory = "batchContainerFactory")
//    public void receuve(String json){
//        if (StringUtils.isEmpty(json)) {
//            log.error("kafka数据为空");
//            return;
//        }
//        Task task = null;
//        try {
//            task = OM.readValue(json, Task.class);
//        } catch (Exception e) {
//            e.printStackTrace();
//            log.error("task反序列化失败:{}", e);
//        }
//        if(!this.checkTask(task)){
//            return;
//        }
//        consume(task);
//
//    }
//
//    public boolean checkTask(Task task) {
//        if (CollectionUtils.isEmpty(task.getParams())) {
//            return false;
//        }
//        //task的Type不能为空
//        if (StringUtils.isEmpty(task.getType())) {
//            return false;
//        }
//        //new不能为空
//        if (task.getNext() == null) {
//            return false;
//        }
//        Task nextTask = task.getNext();
//        //nextTask的Type不能为空
//        if (StringUtils.isEmpty(nextTask.getType())) {
//            return false;
//        }
//        //next的callback不能为空
//        if (!nextTask.getParams().containsKey("url") || StringUtils.isEmpty(nextTask.getParams().get("url"))) {
//            return false;
//        }
//        //设备号不能为空
//        Map<String, String> equipmentIdsMap = task.getParams();
//        if (!equipmentIdsMap.containsKey(EquipmentReceiveConstant.DeviceCode) || StringUtils.isEmpty(EquipmentReceiveConstant.DeviceCode)) {
//            return false;
//        }
//        //经纬度不能为空
//        if (!equipmentIdsMap.containsKey(EquipmentReceiveConstant.Latitude) || StringUtils.isEmpty(EquipmentReceiveConstant.Latitude)) {
//            return false;
//        }
//        if (!equipmentIdsMap.containsKey(EquipmentReceiveConstant.Longitude) || StringUtils.isEmpty(EquipmentReceiveConstant.Longitude)) {
//            return false;
//        }
//        return true;
//
//    }
//    public void consume(Task task) {
//        electricFenceHandler.checkElectricFence(task);
//    }
}
